// import { queryTags } from '../services/api';
import { fetchTagList, addTag, deleteTag, editTag } from '../services/contentService';
import { decryptRes } from '../utils/requestUtil';
import { message } from 'antd';

export default {
  namespace: 'tag',

  state: {
    tagList: [],
  },

  effects: {
    *fetchTagList({ payload }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(fetchTagList, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'tagListReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *addTag({ payload, callback }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(addTag, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        message.success('添加成功！')
        if(callback) callback()
      } else {
        message.error('标签名不能重复');
      }
    },

    *deleteTag({ payload, callback }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(deleteTag, { labelId: payload.labelId })));

      if (response.code === 0) {
        message.success('删除成功！')
        if(callback) callback()
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *editTag({ payload, callback }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(editTag, payload)));
      console.log(response)
      if (response.code === 0) {
        message.success('修改成功！')
        if(callback) callback()
      } else {
        message.error('标签名不能重复');
      }
    },
  },



  reducers: {
    tagListReducer(state, action) {
      return {
        ...state,
        tagList: action.payload.data,
      };
    },
  },
};
